const app = getApp()
const requireHttps = app.globalData.requireHttps //获取require域名
const WXBizDataCrypt = require('../../utils/RdWXBizDataCrypt.js');
Page({

  data: {
    hideShare: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userPhone: '',
    item: [],
    audit_status: '',
    card: '',
    codeImgPath: '',
    code_imgPath: [],
    userHead: '',
    userName: '',
    is_show: 'hide',
    claim_num: ''
  },
  //返回首页
  onUnload: function() {
    // wx.navigateBack()
    // wx.navigateTo({
    //   url: '/pages/index/index',
    // })
  },
  onLoad: function() {
    let _this = this;
  },


  onShow: function() {
    let _this = this;
    _this.getUserinfos()
    _this.setData({
      userHead: app.globalData.userHead,
      userName: app.globalData.userName,
      codeImgPath: app.globalData.codeImgPath,
      money: app.globalData.balance,
      service_call: app.globalData.service_call,
      sex: app.globalData.sex
    })
  },

  //显示隐藏分享二维码
  colseShare: function() {
    let _this = this;
    _this.setData({
      hideShare: (!this.data.hideShare)
    })
  },

  //跳转绑定车辆页面
  goMyCar: function() {
    let _this = this;
    if (app.globalData.card.length == 0 || app.globalData.card.length == '' || app.globalData.card == undefined) {
      wx.navigateTo({
        url: '/pages/bindVehicle/bindVehicle',
      })
    } else {
      wx.navigateTo({
        url: '/pages/bindVehicleYes/bindVehicleYes',
      })
    }
  },

  //跳转我的车位页面
  goMyParking: function() {
    let _this = this
    if (_this.data.claim_num == 0) {
      wx.navigateTo({
        url: '/pages/myParking/myParking',
      })
    } else {
      wx.navigateTo({
        url: '/pages/myParkingHave/myParkingHave',
      })
    }

  },

  //合作物业
  goProperty: function() {
    let _this = this;
    if (app.globalData.audit_status == 3) {
      wx.showToast({
        title: '敬请期待',
        icon: 'none',
        mask: false
      })
    } else {
      wx.navigateTo({
        url: '/pages/property/property',
      })
    }

    // if (app.globalData.audit_status == 0) {
    //   wx.navigateTo({
    //     url: '/pages/property/property',
    //   })
    // } else if (app.globalData.audit_status == 1) {
    //   wx.navigateTo({
    //     url: '/pages/inAudit/inAudit',
    //   })
    // } else if (app.globalData.audit_status == 2) {
    //     // wx.navigateTo({
    //     //   url: '/pages/auitSuccess/auitSuccess',
    //     // })
    //     wx.navigateTo({
    //       url: '/pages/myAudit/myAudit',
    //     })
    // } else if (app.globalData.audit_status == 3) {
    //   wx.showToast({
    //     title: '敬请期待',
    //     icon: 'none',
    //     mask: false
    //   })
    // } else if (app.globalData.audit_status == 4) {
    //   wx.navigateTo({
    //     url: '/pages/findAudit/findAudit',
    //   })
    // } else if (app.globalData.audit_status == '' || app.globalData.audit_status == null || app.globalData.audit_status == undefined) {
    //   wx.showToast({
    //     title: '敬请期待',
    //     icon: 'none',
    //     mask: false
    //   })
    // }
  },

  //查看明细
  seeDetail: () => {
    wx.navigateTo({
      url: '/pages/detailed/detailed',
    })
  },

  //充值
  goRecharge: () => {
    wx.navigateTo({
      url: '/pages/recharge/recharge',
    })
  },

  //提现
  goCarryCash: () => {
    wx.navigateTo({
      url: '/pages/carryCash/carryCash',
    })
  },

  //我的订单
  goMyOrder: () => {
    wx.navigateTo({
      url: '/pages/myOrder/myOrder',
    })
  },

  //联系客服
  callServer: () => {
    console.log(app.globalData.callServer)
    wx.makePhoneCall({
      phoneNumber: app.globalData.callServer,
      success: () => {
        console.log('拨打成功')
      },
      fail: () => {
        console.log('拨打失败')
      }
    })
  },

  //分享二维码
  onShareAppMessage: function() {
    let _this = this;
    // console.log(_this.data.codeImgPath)
    // code_imgPath.push(_this.data.codeImgPath)
    // console.log(code_imgPath)
    // wx.previewImage({
    //   current: _this.data.codeImgPath, // 当前显示图片的http链接   
    //   urls: code_imgPath // 需要预览的图片http链接列表   
    // })
    // " + _this.data.codeImgPath + "
    return {
      title: "转发给好友",
      imageUrl: "" + _this.data.codeImgPath + "",
      path: "/pages/index/index"
    }
  },
  //用户点击授权按钮
  onGetUserInfo: function() {
    let _this = this;
    wx.getSetting({
      success: res => {
        // console.log(res)
        if (res.authSetting['scope.userInfo']) {
          wx.showLoading({
            title: '正在加载',
            mask: true
          })
          // 已经授权，调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: res => {
              // console.log(res.userInfo)
              // app.globalData.userHead = res.userInfo.avatarUrl
              // app.globalData.userName = res.userInfo.userName
              //获取用户信息
              _this.setData({
                is_show: 'hide'
              })

              // 登录
              //调用微信登录接口，获取code
              wx.login({
                success: res => {
                  //登录凭证
                  let code = res.code;
                  if (code) {

                    //传入token获取openid
                    wx.request({
                      url: '' + requireHttps + '/api/wechat/getToken',
                      data: {
                        code: code
                      },
                      method: "post",
                      success: res => {
                        // console.log(res);
                        //存储openid
                        app.globalData.openid = res.data.data.openid
                        //存储session_key
                        app.globalData.session_key = res.data.data.session_key
                        //解密用户信息获取openid unionid
                        let pc = new WXBizDataCrypt(app.globalData.APP_ID, app.globalData.session_key)

                        //获取用户信息
                        wx.getUserInfo({
                          success: res => {
                            // console.log(res);
                            app.globalData.userHead = res.userInfo.avatarUrl
                            app.globalData.userName = res.userInfo.nickName
                            //定义变量接收解密后的encryptedData
                            let data = pc.decryptData(res.encryptedData, res.iv)
                            // console.log(data)
                            // app.globalData.unionid = data.unionId
                            let head = data.avatarUrl;
                            let name = data.nickName;
                            let sex = res.userInfo.gender;
                            let phone = '';
                            let code_token = code;

                            //用户注册
                            wx.request({
                              url: '' + requireHttps + '/api/wechat/userReg',
                              data: {
                                head: head,
                                name: name,
                                sex: sex,
                                phone: phone,
                                openid: app.globalData.openid,
                                unionid: '',
                                code_token: code
                              },
                              method: 'post',
                              success: res => {
                                // console.log(res);
                                //注册成功获取用户token
                                app.globalData.token = res.data.data.token;
                                // _this.getMyOrderStatus()
                                //获取用户信息
                                wx.request({
                                  url: '' + requireHttps + '/api/member/getMemberInfo',
                                  data: {
                                    token: app.globalData.token
                                  },
                                  method: 'post',
                                  success: res => {
                                    //存储合作物业审核状态
                                    app.globalData.audit_status = res.data.data.audit_status;
                                    //存储我的车牌号
                                    app.globalData.card = res.data.data.card;
                                    //存储客服电话
                                    app.globalData.callServer = res.data.data.service_call;
                                    //存储用户余额
                                    app.globalData.balance = res.data.data.money;
                                    app.globalData.codeImgPath = res.data.data.code_img;

                                    wx.hideLoading();
                                    //存储用户信息
                                    _this.setData({
                                      head: res.data.data.head,
                                      code_img: res.data.data.code_img,
                                      money: app.globalData.balance,
                                      name: res.data.data.name,
                                      phone: res.data.data.phone,
                                      service_call: app.globalData.service_call,
                                      sex: app.globalData.sex
                                    })
                                  }
                                })
                              }
                            })
                          }
                        })
                      }
                    })
                  }
                },
                fail: () => {
                  console.log('登陆失败')
                }
              })
            }
          })
        } else {
          //没有授权显示按钮
          _this.setData({
            is_show: ''
          })
        }
      }
    })
  },
  //获取用户信息
  getUserinfos: function() {
    let _this = this

    //获取用户信息
    wx.request({
      url: '' + requireHttps + '/api/member/getMemberInfo',
      data: {
        token: app.globalData.token
      },
      method: 'post',
      success: res => {
        // console.log(res);
        if (res.data.message == 'token已失效') {
          _this.onGetUserInfo()
        }
        //存储合作物业审核状态
        // _this.data.audit_status = res.data.data.audit_status;
        //存储我的车牌号
        // app.globalData.card = res.data.data.card;
        //存储客服电话
        // app.globalData.callServer = res.data.data.service_call;
        //存储用户余额
        app.globalData.balance = res.data.data.money;
        // _this.data.codeImgPath = res.data.data.code_img;
        //存储我的车位是否有数据
        _this.data.claim_num = res.data.data.claim_num
        app.globalData.auditStatus = res.data.data.audit_status
        wx.hideLoading();
        //存储用户信息
        _this.setData({
          head: res.data.data.head,
          code_img: res.data.data.code_img,
          money: app.globalData.balance,
          name: res.data.data.name,
          phone: res.data.data.phone,
          service_call: res.data.data.service_call,
          sex: res.data.data.sex
        })
      }
    })
  },
  updatePhone: function(e) {
    let _this = this
    console.log(e)
    console.log(e.detail.value)
    _this.data.phone = e.detail.value
  },
  userUpdateBlur: function() {

    let _this = this
    if (_this.data.phone.length != 11) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号码',
      })
      return false
    } else {
      wx.showModal({
        title: '提示',
        content: '您确认使用此手机号吗？',
        success: res => {
          if (res.confirm) {
            wx.request({
              url: '' + requireHttps + '/api/member/modifyUserPhone',
              data: {
                token: app.globalData.token,
                phone: _this.data.phone
              },
              method: 'post',
              success: res => {
                // console.log(res)

                wx.showToast({
                  title: '添加成功',
                })
              }
            })
          } else if (res.cancel) {

          }
        }
      })
    }
  }
})